package main

import (
	"GoApi/global"
	"GoApi/handle"
	_ "GoApi/docs"
	"context"
	"fmt"
	"os"
	"os/signal"
	"time"

	"github.com/labstack/echo/v4"
	"github.com/labstack/echo/v4/middleware"
	"github.com/labstack/gommon/log"
	echoSwagger "github.com/swaggo/echo-swagger"
)
// @title       HermitChain_CrossChain
// @version     v0.0.1
// @description 统一请求返回格式：{\"data\":dataObject,\"msg\":\"消息内容\",\"ret\":请求状态码}\r\n ret：\r\n200、成功\r\n400、失败\r\n401、非法用户\r\n402、签名验证失败\r\n403、交易哈希错误

// @BasePath /api
// @name     Authorization
func main() {
	defer func() {
		if err := recover(); err != nil {
			println("run error：%v", err)
			time.Sleep(1 * time.Minute)
		}
	}()
	err := global.InitGlobal(&global.ConfPath{
		ConfigPath: "config/config.toml",
	})
	if err != nil {
		println("Initialization fail：", err.Error())
		time.Sleep(1 * time.Minute)
		return
	}

	e := echo.New()
	e.Logger.SetLevel(log.INFO)
	e.Use(middleware.Logger())
	e.Use(middleware.Recover())
	e.Use(middleware.RequestID())
	e.Use(middleware.Secure())
	e.Use(middleware.CORS())
	e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
		AllowOrigins: []string{"*"},
		AllowMethods: []string{echo.GET, echo.HEAD, echo.PUT, echo.PATCH, echo.POST, echo.DELETE},
	}))
	e.GET("/swagger/*", echoSwagger.WrapHandler)
	//添加swagger访问路由
	// e.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

	api := e.Group("/api")
	{
		// 创建跨链订单
		api.POST("/crosschain/createhytask", handle.CreateHyTask)
		// 获取EVM跨链任务
		api.POST("/crosschain/getcrosschaintask", handle.GetCrossChaintask)
		// 获取Cosmos跨链任务
		api.POST("/crosschain/getcosmoschaintask", handle.GetCosmosChainTask)
		// 更新跨链订单
		api.POST("/crosschain/updatehytask", handle.UpdateHyTask)
		// 查询跨链订单
		api.POST("/crosschain/gethyorder", handle.GetHyOrder)
		// 查询跨链订单列表
		api.POST("/crosschain/gethyOrderlist", handle.GetHyOrderList)
		// 查询链GAS费
		api.POST("/crosschain/getchaingasFee", handle.GetChainGasFee)
		// 获取支持跨链的NFT合集列表
		api.POST("/crosschain/nftcontractlist", handle.NftContractList)
		// 查看token价格
		api.POST("/crosschain/gettokenprice", handle.GetToeknPrice)
	}

	go func() {
		if err := e.Start(fmt.Sprintf(":%d", global.Conf.HTTP.Port)); err != nil {
			e.Logger.Info("shutting down the server")
		}
	}()
	// Wait for interrupt signal to gracefully shutdown the server with
	// a timeout of 10 seconds.
	quit := make(chan os.Signal)
	signal.Notify(quit, os.Interrupt)
	<-quit
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	if err := e.Shutdown(ctx); err != nil {
		e.Logger.Fatal(err)
	}
}
